#include <QDebug>
#include <QDir>

#include "hybridplugin.h"
#include "iqtprojectgenerator.h"

HybridPlugin::HybridPlugin()
{
}

HybridPlugin::~HybridPlugin()
{
}

const QString& HybridPlugin::name() const
{
    return m_name;
}

void HybridPlugin::setName(QString name)
{
    m_name = name;
}

const QString& HybridPlugin::version() const
{
    return m_version;
}

void HybridPlugin::setVersion(QString version)
{
    m_version = version;
}

const QList< QSharedPointer<HybridPluginPlatform> >& HybridPlugin::platforms() const
{
    return m_platforms;
}

void HybridPlugin::addPlatform(QSharedPointer<HybridPluginPlatform> platform)
{
    m_platforms.append(platform);
}

void HybridPlugin::selectPlatform(QSharedPointer<HybridPluginPlatform> platform)
{
    m_selectedPlatform = platform;
}

QSharedPointer<HybridPluginPlatform> HybridPlugin::selectedPlatform() const
{
    return m_selectedPlatform;
}

QSharedPointer<HybridPluginPlatform> HybridPlugin::conformingVersion(const PlatformType& type)
{
    // Clear previous selection
    m_selectedPlatform = QSharedPointer<HybridPluginPlatform>();

    qDebug() << "Selecting platform for plugin " << name() << ", platform "
             << type.m_platformName << type.m_platformVersion
             << ", Qt " << type.m_qtVersion;

    // Check for libs
    foreach(QSharedPointer<HybridPluginPlatform> platform, m_platforms)
    {
        if (platform->name().toLower() == type.m_platformName.toLower() &&
            platform->format() == HybridPluginPlatform::Library)
        {
            // In lib format, let's stick to strict versions both in
            // the platform and in Qt
            if (platform->version() == type.m_platformVersion &&
                platform->qtVersion() == type.m_qtVersion)
            {
                m_selectedPlatform = platform;
            }
        }
    }

    // Check for sources
    if (!m_selectedPlatform)
    {
        foreach(QSharedPointer<HybridPluginPlatform> platform, m_platforms)
        {
            if (platform->name().toLower() == type.m_platformName.toLower() &&
                platform->format() == HybridPluginPlatform::Source)
            {
                // With source we're for now going to assume there are no
                // restrictions in Qt or platform versions
                m_selectedPlatform = platform;
            }
        }
    }

    if (m_selectedPlatform)
    {
        qDebug() << (m_selectedPlatform->format() == HybridPluginPlatform::Source ? "source" : "lib");
        qDebug() << (m_selectedPlatform->configuration() == HybridPluginPlatform::Release ? "release" : "debug");
    }
    else
    {
        qDebug() << "No suitable platform found";
    }

    // Null default representing no compatible versions
    return m_selectedPlatform;
}

QString HybridPlugin::symbianDeploymentString(bool isRemotelyCompiled)
{
    // Precompiled library
    // Indentation weirdness to make generated project source more readable
    //@name@api-dll = \"   \\\"$$PWD/mw/@name@/@url@\\\" - \\\"!:\\sys\\bin\\@name@api.dll\\\"\"\n\
    //@name@api-qtplugin.pkg_postrules += @name@api-dll\n\
    //@name@api-qtplugin.pkg_postrules += @name@api-plugin\n\
    //@name@api-qtplugin.sources = mw\\@name@\\@url@\n\
    //@name@api-qtplugin.path = \\resource\\qt\\plugins\\hybridplugins\n\
    //@name@api-stub.sources = mw\\@name@\\qmakepluginstubs\\@name@api.qtplugin\n\
    //@name@api-stub.path = \\resource\\qt\\plugins\\hybridplugins\n\
    //@name@api-plg = \"   \\\"$$PWD/qmakepluginstubs/@name@api.qtplugin\\\" - \\\"!:\\resource\\qt\\plugins\\hybridplugins\\@name@api.qtplugin\\\"\"\n\
    //@name@api-qtplugin.pkg_postrules += @name@api-plg\n\
    //DEPLOYMENT += @name@api-stub\n\
    
   
    //@name@api-qtplugin.sources = mw/@name@/@url@\n\
    //@name@api-qtplugin.path = \\resource\\qt\\plugins\\hybridplugins\n\
    //@name@api-qtplugin.pkg_prerules += @name@api-plugin\n\
    //@name@api-qtplugin.sources = qmakepluginstubs\\@name@api.qtplugin\n\
    //@name@api-qtplugin.path = \\resource\\qt\\plugins\\hybridplugins\n\
    //
    //@name@api-dll = \"   \\\"$$PWD/mw/@name@/@url@\\\" - \\\"!:\\sys\\bin\\@name@api.dll\\\"\"\n\
    //@name@api-plugin = \"   \\\"$$PWD/qmakepluginstubs\\@name@api.qtplugin\\\" - \\\"!:\\resource\\qt\\plugins\\hybridplugins\\@name@api.qtplugin\\\"\"\n\
    //@name@api-dll = \"   \\\"C:\\wcc\\build\\src\\_\\mw\\@name@\\@url@\\\" - \\\"!:\\sys\\bin\\@name@api.dll\\\"\"\n\
    //@name@api-plugin = \"   \\\"C:\\wcc\\build\\src\\_\\qmakepluginstubs\\@name@api.qtplugin\\\" - \\\"!:\\resource\\qt\\plugins\\hybridplugins\\@name@api.qtplugin\\\"\"\n\

    static const QString remoteLibraryDeployment = "\n\
    @name@api-dll = \"   \\\"C:\\wcc\\build\\src\\_\\mw\\@name@\\@url@\\\" - \\\"!:\\sys\\bin\\@name@api.dll\\\"\"\n\
    @name@api-plugin = \"   \\\"C:\\wcc\\build\\src\\_\\qmakepluginstubs\\@name@api.qtplugin\\\" - \\\"!:\\resource\\qt\\plugins\\hybridplugins\\@name@api.qtplugin\\\"\"\n\
    @name@api-qtplugin.pkg_prerules += @name@api-dll\n\
    @name@api-qtplugin.pkg_prerules += @name@api-plugin\n\
    DEPLOYMENT += @name@api-qtplugin\n";

    static const QString localLibraryDeployment = "\n\
    @name@api-dll = \"   \\\"$$PWD/mw/@name@/@url@\\\" - \\\"!:\\sys\\bin\\@name@api.dll\\\"\"\n\
    @name@api-qtplugin.pkg_postrules += @name@api-dll\n\
    @name@api-qtplugin.sources = qmakepluginstubs\\@name@api.qtplugin\n\
    @name@api-qtplugin.path = \\resource\\qt\\plugins\\hybridplugins\n\
    DEPLOYMENT += @name@api-qtplugin\n";

    // Library compiled from sources
    static const QString sourceDeployment = "\n\
    @name@api-dll.sources = \\epoc32\\release\\$(PLATFORM)\\$(CFG)\\@name@api.dll\n\
    @name@api-dll.path = \\sys\\bin\n\
    DEPLOYMENT += @name@api-dll\n\
    @name@api-qtplugin.sources = mw\\@name@\\qmakepluginstubs\\@name@api.qtplugin\n\
    @name@api-qtplugin.path = \\resource\\qt\\plugins\\hybridplugins\n\
    DEPLOYMENT += @name@api-qtplugin\n";

    if (m_selectedPlatform->format() == HybridPluginPlatform::Library) {
        if (isRemotelyCompiled) {
            return QString(remoteLibraryDeployment)
                    .replace("@name@", name())
                    .replace("@url@", m_selectedPlatform->url());
        } else {
            return QString(localLibraryDeployment)
                    .replace("@name@", name())
                    .replace("@url@", m_selectedPlatform->url());
        }
    } else if (m_selectedPlatform->format() == HybridPluginPlatform::Source) {
        return QString(sourceDeployment).replace("@name@", name());
    } else {
        return "";
    }
}
